package com.chuzhiyun.chengguoxy.feature.internship.worktags;

import android.content.Intent;
import android.os.Build;
import android.os.Bundle;

import androidx.annotation.RequiresApi;
import androidx.fragment.app.Fragment;
import androidx.recyclerview.widget.GridLayoutManager;

import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import com.chad.library.adapter.base.BaseQuickAdapter;
import com.chad.library.adapter.base.viewholder.BaseViewHolder;
import com.chuzhiyun.chengguoxy.R;
import com.chuzhiyun.chengguoxy.databinding.FragmentWorkTagsInfoBinding;
import com.chuzhiyun.chengguoxy.feature.base.BaseFragment;
import com.chuzhiyun.chengguoxy.feature.bean.LeavelistBean;
import com.chuzhiyun.chengguoxy.feature.bean.WorkTagsListBean;
import com.chuzhiyun.chengguoxy.feature.internship.leave.LeaveListFragment;
import com.chuzhiyun.chengguoxy.network.NetApi;
import com.chuzhiyun.chengguoxy.network.NetService;
import com.chuzhiyun.chengguoxy.network.StringNetCallback;
import com.chuzhiyun.chengguoxy.utlis.CustomClickListener;
import com.chuzhiyun.chengguoxy.utlis.NewItemClickListener;
import com.chuzhiyun.chengguoxy.utlis.TimeUtils;
import com.google.gson.Gson;

import org.jetbrains.annotations.NotNull;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;

public class WorkTagsInfoFragment extends BaseFragment<FragmentWorkTagsInfoBinding> {

    private String tasksId;
    private String tasksName;

    private final List<WorkTagsListBean.ResultsBean> dataSource = new ArrayList<WorkTagsListBean.ResultsBean>();
    private WorkTagsInfoFragment.MyAdapter myAdapter;
    @Override
    public void initView(View view) {
        binding.navView.ivBack.setOnClickListener(new CustomClickListener() {
            @Override
            protected void onSingleClick() {
                requireActivity().finish();
            }
        });

        binding.recylerView.setLayoutManager(new GridLayoutManager(getActivity(), 1));
        myAdapter = new WorkTagsInfoFragment.MyAdapter(R.layout.item_worktagslist, dataSource);
        binding.recylerView.setAdapter(myAdapter);
        myAdapter.setOnItemClickListener(new NewItemClickListener() {
            @Override
            protected void onSingleClick(BaseQuickAdapter adapter, View view, int position) {
                Intent intent = new Intent(getContext(), WorkTasksInfoActivity.class);
                intent.putExtra("tasksId", tasksId);
                String id = dataSource.get(position).getId()+"";
                intent.putExtra("id", id);
                intent.putExtra("tasksName",tasksName);
                startActivity(intent);
            }
        });
    }

    @Override
    public void initData() {

    }

    @Override
    public void setListener() {
        Bundle bundle = getArguments();
        tasksId = bundle != null ? bundle.getString("tasksId") : null;
        System.out.println(tasksId);
        tasksName =  bundle != null ? bundle.getString("tasksName") : null;
        binding.navView.tvTitle.setText(tasksName);
        getWorkTasksList();
    }

    private void getWorkTasksList(){
        String url = NetApi.getTasksList();
        HashMap<String,String>param = new HashMap<>();
        param.put("task",tasksId);
        NetService.getInstance().getParam(url, param, new StringNetCallback() {
            @Override
            public void onSuccess(String response, int statusCode) throws Exception {
                System.out.println(response);
                if (statusCode==200){
                    Gson gson = new Gson();
                    WorkTagsListBean bean = gson.fromJson(response,WorkTagsListBean.class);
                    dataSource.addAll(bean.getResults());
                    myAdapter.notifyDataSetChanged();
                }
            }

            @Override
            public void onError(String error) {
                System.out.println(error);
            }
        });
    }

    public static class MyAdapter extends BaseQuickAdapter<WorkTagsListBean.ResultsBean, BaseViewHolder> {

        public MyAdapter(int layoutResId, List<WorkTagsListBean.ResultsBean> data) {
            super(layoutResId, data);
        }

        @RequiresApi(api = Build.VERSION_CODES.O)
        @Override
        protected void convert(@NotNull BaseViewHolder helper, WorkTagsListBean.ResultsBean item) {
            helper.setText(R.id.tasksTitle_Text,"标题");
            String oldStr = item.getLastModified().substring(0,19);
            helper.setText(R.id.createdAt_Text,TimeUtils.convertDate(oldStr));
        }
    }

}